﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComLib.ConfigService;
using System.Reflection;
using System.IO;

namespace PhotoNamingService
{
    public class ProductConfig : IniConfigBase
    {
        #region static field and functions
        private static object _lock = new object();
        protected static ProductConfig _instance;
        public static ProductConfig Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new ProductConfig();
                        }
                    }
                }
                return _instance;
            }
        }

        static ProductConfig()
        {
            ProductConfig config = Instance;
            config.CheckAndCreatConfigFile();
        }

        public static void SetConfig(string deviceType, string deviceID, string cityCode, string localID,string takePhotoType)
        {
            _instance.DeviceType = deviceType;
            _instance._deviceID = deviceID;
            _instance._cityCode = cityCode;
            _instance._localId = localID;
            _instance._takePhotoType = takePhotoType;
            _instance.Save();
        }

        #endregion

        private ProductConfig()
        {

        }

        /// <summary>
        /// 检查默认的配置文件是否存在，不存在则创建
        /// </summary>
        public void CheckAndCreatConfigFile()
        {
            if (File.Exists(ConfigFilePath))
            {
                Load();
                return;
            }
            Save();
            FileInfo info = new FileInfo(ConfigFilePath);
            if (info.Exists)
            {
                info.Attributes = FileAttributes.Hidden;
            }
        }

        /// <summary>
        /// 检查默认的配置文件是否存在，不存在则创建
        /// </summary>
        private string GetDefaultDirectory()
        {
            DriveInfo[] allDirves = DriveInfo.GetDrives();
            if (Directory.Exists("D:\\"))
            {
                return "D:\\";
            }
            if (Directory.Exists("C:\\"))
            {
                return "C:\\";
            }
            throw new Exception("没有找到磁盘根目录D:\\或C:\\");
        }

        public override string ConfigDirectory
        {
            get
            {
                return GetDefaultDirectory();
            }
        }

        public override string ConfigFilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_strConfigPath))
                {
                    _strConfigPath = Path.Combine(ConfigDirectory, this.GetType().Name + ".ini");
                }
                return _strConfigPath;
            }
        }

        /// <summary>
        /// 最新日期
        /// </summary>
        private string _lastDate = DateTime.Now.ToString("yyMMdd");
        public string LastDate
        {
            get { return _lastDate; }
            set { _lastDate = value; }
        }

        /// <summary>
        /// 设备编号(唯一)
        /// </summary>
        private string _deviceID = "0000";
        public string DeviceID
        {
            get { return _deviceID; }
        }
 
        /// <summary>
        /// 当地行政编码
        /// </summary>
        private string _cityCode = "000000";
        public string CityCode
        {
            get { return _cityCode; }
        }

        /// <summary>
        /// 当地设备编号(一位字母或者数字)，多台设备共存的情形下
        /// </summary>
        private string _localId = "0";
        public string LocalId
        {
            get { return _localId; }
        }


        /// <summary>
        /// 设备型号
        /// </summary>
        private string _deviceType = "IPC";
        public string DeviceType
        {
            get { return _deviceType; }
            set { _deviceType = value; }
        }

        private string _takePhotoType = "0";
        public string TakePhotoType
        {
            get { return _takePhotoType; }
            set { _takePhotoType = value; }
        }

        public override bool Save()
        {
            base.Save();
            SaveField("DeviceID", _deviceID);
            SaveField("CityCode", _cityCode);
            SaveField("LocalId", _localId);
            SaveField("DeviceType", _deviceType);
            SaveField("TakePhotoType", _takePhotoType);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                _deviceType = LoadStringValue("DeviceType");
                _deviceID = LoadStringValue("DeviceID");
                _cityCode = LoadStringValue("CityCode");
                _localId = LoadStringValue("LocalId");
                _takePhotoType = LoadStringValue("TakePhotoType");   
            }
            catch (Exception ex)
            {
                _error = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("设备客户端选项: ");
            builder.Append("; [设备编号]:" + _deviceID);
            builder.Append("; [当地设备编号]:" + _cityCode);
            builder.Append("; [行政区域代码]:" + _localId);
            builder.Append("; [设备型号]:" + _deviceType);
            builder.Append("; [拍照类型]:" + _takePhotoType);
            return builder.ToString();
        }
    }
}
